// mine-component.js
Component({
  properties: {
    userId: {
      type: String,
      value: ''
    },
    username: {
      type: String,
      value: ''
    },
    nickname: {
      type: String,
      value: ''
    },
  },
  data: {
    // username: '',
    avatar: '/images/avatar.jpg',
    collectCount: 0,
    wishCount: 0,
    BASE_URL: "http://127.0.0.1:9100/api",
  },
  lifetimes: {
    attached() {
      // 恢复登录状态
      const app = getApp();
      const userId = wx.getStorageSync('userId');
      const token = wx.getStorageSync('token');
      if (userId && token) {
        app.globalData.userId = userId;
        app.globalData.token = token;
      }

      this.getCollectThingList();
      this.getWishThingList();
      // 调用获取用户头像的方法
      this.getUserAvatar();
    }
  },
  methods: {
    getCollectThingList() {
      const app = getApp();
      const userId = app.globalData.userId;
      const token = app.globalData.token;
      const BASE_URL = this.data.BASE_URL;
      if (!userId || !token) {
        console.log('未登录，无法获取心愿单数量');
        return;
      }
      // 这里需要替换为实际的小程序请求接口代码
      wx.request({
        url: `${BASE_URL}/thingCollect/getUserCollectList?userId=${userId}`,
        method: 'GET',
        header: {
          'TOKEN': token,
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: userId
        },
        success: (res) => {
          console.log(res.data);
          console.log(res.data.length);
          this.setData({
            collectCount: res.data.data.length
          });
        },
        fail: (err) => {
          console.log(err.errMsg);
        }
      });
    },
    getWishThingList() {
      const app = getApp();
      const userId = app.globalData.userId;
      const token = app.globalData.token;
      const BASE_URL = this.data.BASE_URL;
      if (!userId || !token) {
        console.log('未登录，无法获取收藏数量');
        return;
      }
      // 这里需要替换为实际的小程序请求接口代码
      wx.request({
        url: `${BASE_URL}/thingWish/getUserWishList?userId=${userId}`,
        method: 'GET',
        header: {
          'TOKEN': token,
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: userId
        },
        success: (res) => {
          console.log('完整的响应数据:', res); // 打印完整的响应数据
          if (res.data && Array.isArray(res.data.data)) {
            this.setData({
              wishCount: res.data.data.length
            });
          } else {
            console.log('数据格式不符合预期:', res.data);
          }
        },
        fail: (err) => {
          console.log(err.errMsg);
        }
      });
    },
    // 获取用户头像的方法
    getUserAvatar() {
      const app = getApp();
      const userId = app.globalData.userId;
      const token = app.globalData.token;
      if (!userId || !token) {
        console.log('未登录，无法获取头像');
        return;
      }
      const BASE_URL = this.data.BASE_URL;
      wx.request({
        url: `${BASE_URL}/user/detail?userId=${userId}`,
        method: 'GET',
        header: {
          'TOKEN': token,
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: (res) => {
          if (res.data.code === 200) {
            const user = res.data.data;
            const username = user.username;
            const nickname = user.nickname;
            if (user.avatar) {
              const avatarUrl = `${BASE_URL}/staticfiles/avatar/${user.avatar}`;
              this.setData({
                avatar: avatarUrl,
                username,
                nickname
              });
            }
          } else {
            wx.showToast({
              title: '获取头像失败',
              icon: 'none'
            });
          }
        },
        fail: (err) => {
          wx.showToast({
            title: '请求失败',
            icon: 'none'
          });
          console.log(err.errMsg);
        }
      });
    },
    handleMenuItemClick(e) {
      const item = e.currentTarget.dataset.item;
      switch (item) {
        case 'collectThingView':
          // 跳转到收藏页面
          wx.navigateTo({
            url: '/pages/collectThingView/collectThingView'
          });
          break;
        case 'wishThingView':
          // 跳转到心愿单页面
          wx.navigateTo({
            url: '/pages/wishThingView/wishThingView'
          });
          break;
        case 'orderView':
          // 跳转到订单列表页面
          wx.navigateTo({
            url: '/pages/orderView/orderView'
          });
          break;
        case 'commentView':
          // 跳转到评论页面
          wx.navigateTo({
            url: '/pages/commentView/commentView'
          });
          break;
        case 'userInfoEditView':
          // 跳转到个人资料页面
          wx.navigateTo({
            url: '/pages/userInfoEditView/userInfoEditView'
          });
          break;
        case 'securityView':
          // 跳转到账户安全页面
          wx.navigateTo({
            url: '/pages/securityView/securityView'
          });
          break;
        case 'messageView':
          // 跳转到消息管理页面
          wx.navigateTo({
            url: '/pages/messageView/messageView'
          });
          break;
        case 'logout':
          wx.showModal({
            title: '提示',
            content: '确定要退出登录吗？',
            success: function (res) {
              if (res.confirm) {
                // 获取全局变量
                const app = getApp();
                // 清除全局的 userId 和 token
                app.globalData.userId = null;
                app.globalData.token = null;
                // 清除本地存储
                wx.clearStorageSync();
                // 跳转到登录页面
                wx.navigateTo({
                  url: '/pages/login/login'
                });
              }
            }
          });
          break;
      }
    },
    navigateToCollectOrWish(e) {
      const type = e.currentTarget.dataset.type;
      const app = getApp();
      const userId = app.globalData.userId;
      wx.navigateTo({
        url: `/pages/collectOrWish/collectOrWish?type=${type}&userId=${userId}`
      });
    }
  }
});